package org.sadhar.sipp.posbiaya;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassUtility;
import org.springframework.jdbc.core.RowMapper;

public class PosBiayaDAOImpl implements PosBiayaDAO {

    public PosBiayaDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    @Override
    public List<PosBiaya> list(String keySearch) throws Exception {
        List<PosBiaya> list = new ArrayList<PosBiaya>();
        List<Map> rows = ClassConnection.getJdbc().queryForList(
                "SELECT * FROM ppmk.posBiaya WHERE namaPosBiaya LIKE ?",
                new Object[]{"%" + keySearch + "%"});
        for (Map m : rows) {
            PosBiaya pos = new PosBiaya();
            pos.setKodePosBiaya(ClassAntiNull.AntiNullInt(m.get("kodePosBiaya")));
            pos.setNamaPosBiaya(ClassAntiNull.AntiNullString(m.get("namaPosBiaya")));
            pos.setNamaTableItem(ClassAntiNull.AntiNullString(m.get("namaTabelItem")));
            pos.setNamaTableSubItem(ClassAntiNull.AntiNullString(m.get("namaTabelSubItem")));
            pos.setKodeKelompokPengurusBiaya(ClassAntiNull.AntiNullInt(m.get("kodeKelompokPengurusBiaya")));
            pos.setIsMutlak(ClassAntiNull.AntiNullString("isMutlak"));
            list.add(pos);
        }
        return list;
    }

    @Override
    public PosBiaya select(int kode) throws Exception {
        String sql = "SELECT * FROM ppmk.posBiaya WHERE kodePosBiaya=?";
        return (PosBiaya) ClassConnection.getJdbc().queryForObject(sql,
                new Object[]{Integer.valueOf(kode)}, new RowMapper() {

            public Object mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                return new PosBiaya(rs.getInt("kodePosBiaya"), rs.getString("namaPosBiaya"), rs.getString("namaTabelItem"), rs.getString("namaTabelSubItem"), rs.getInt("kodeKelompokpengurusBiaya"), rs.getString("isMutlak"));
            }
        });
    }

    @Override
    public PosBiaya select(String nama) throws Exception {
        String sql = "SELECT * FROM ppmk.posBiaya WHERE namaPosBiaya=?";
        return (PosBiaya) ClassConnection.getJdbc().queryForObject(sql,
                new Object[]{ClassUtility.quote(nama)}, new RowMapper() {

            public Object mapRow(ResultSet rs, int rowNum)
                    throws SQLException {
                return new PosBiaya(rs.getInt("kodePosBiaya"), rs.getString("namaPosBiaya"), rs.getString("namaTabelItem"), rs.getString("namaTabelSubItem"), rs.getInt("kodeKelompokpengurusBiaya"), rs.getString("isMutlak"));
            }
        });
    }

    public List<PosBiaya> list() throws Exception {
        List<PosBiaya> list = new ArrayList<PosBiaya>();
        List<Map> row = ClassConnection.getJdbc().queryForList(
                "SELECT * FROM ppmk.posBiaya");
        for (Map m : row) {
            PosBiaya pos = new PosBiaya();
            pos.setKodePosBiaya(ClassAntiNull.AntiNullInt(m.get("kodePosBiaya")));
            pos.setNamaPosBiaya(ClassAntiNull.AntiNullString(m.get("namaPosBiaya")));
            pos.setNamaTableItem(ClassAntiNull.AntiNullString(m.get("namaTabelItem")));
            pos.setNamaTableSubItem(ClassAntiNull.AntiNullString(m.get("namaTabelSubItem")));
            pos.setKodeKelompokPengurusBiaya(ClassAntiNull.AntiNullInt(m.get("kodeKelompokPengurusBiaya")));
            pos.setIsMutlak(ClassAntiNull.AntiNullString(m.get("isMutlak")));
            list.add(pos);
        }
        return list;
    }

    public List<PosBiaya> list(String tahunKelompok, int kodeJenisBiaya) throws Exception {
        List<PosBiaya> list = new ArrayList<PosBiaya>();
        List<Map> row = ClassConnection.getJdbc().queryForList("SELECT * FROM ppmk.kelompokBiaya,ppmk.posBiaya " +
                "where ppmk.kelompokBiaya.kodePosBiaya=ppmk.posBiaya.kodePosBiaya and 	" +
                "ppmk.kelompokBiaya.tahunKelompok=? and  ppmk.kelompokBiaya.kodeJenisBiaya=? ", new Object[]{tahunKelompok, kodeJenisBiaya});
        for (Map m : row) {
            PosBiaya pos = new PosBiaya();
            pos.setKodePosBiaya(Integer.valueOf(m.get("kodePosBiaya").toString()));
            pos.setNamaPosBiaya(m.get("namaPosBiaya").toString());
            pos.setNamaTableItem(m.get("namaTabelItem").toString());
            pos.setNamaTableSubItem(m.get("namaTabelSubItem").toString());
            pos.setKodeKelompokPengurusBiaya(Integer.valueOf(m.get("kodeKelompokPengurusBiaya").toString()));
            pos.setIsMutlak(m.get("isMutlak").toString());
            list.add(pos);
        }
        return list;
    }

    public List<PosBiaya> list(int kode1, int kode2) throws Exception {
        List<PosBiaya> list = new ArrayList<PosBiaya>();
        List<Map> row = ClassConnection.getJdbc().queryForList(
                "SELECT * FROM ppmk.posBiaya WHERE kodePosBiaya=? or kodePosBiaya=?",
                new Object[]{kode1, kode2});

        for (Map m : row) {
            PosBiaya posBiaya = new PosBiaya();
            posBiaya.setKodePosBiaya(ClassAntiNull.AntiNullInt(m.get("kodePosBiaya")));
            posBiaya.setNamaPosBiaya(ClassAntiNull.AntiNullString(m.get("namaPosBiaya")));
            posBiaya.setNamaTableItem(ClassAntiNull.AntiNullString(m.get("namaTabelItem")));
            posBiaya.setNamaTableSubItem(ClassAntiNull.AntiNullString(m.get("namaTabelSubItem")));
            posBiaya.setKodeKelompokPengurusBiaya(ClassAntiNull.AntiNullInt(m.get("kodeKelompokPengurusBiaya")));
            posBiaya.setIsMutlak(ClassAntiNull.AntiNullString(m.get("isMutlak")));
            list.add(posBiaya);
        }
        return list;
    }
}
